﻿using NCP.Core;
using NCP.Model;
using NCP.Service;
using NCP.Util;
using SqlSugar;
using System.Collections.Generic;
using System.Web.Mvc;
using WebApp.Filters;

namespace WebApp.Controllers
{
    [Authen]
    public class BaseController<T> : Controller where T : BaseModel, new()
    {
        protected BaseService<T> _baseService;

        public virtual ActionResult Save(T item)
        {
            int rst = _baseService.Save(item);
            LayData ajaxRst = new LayData("0", "保存成功");
            return Content(JsonHelp.SerializeObject(ajaxRst));
        }

        public virtual ActionResult Get(int Id)
        {
            T model = _baseService.Get(Id);
            LayData ajaxRst = new LayData("0", "获取成功", model, 1);
            return Content(JsonHelp.SerializeObject(ajaxRst));
        }

        public virtual ActionResult DelByIds(string Ids)
        {
            bool rst = _baseService.DelByIds(Ids);
            LayData ajaxRst = new LayData("0", "删除成功");
            return Content(JsonHelp.SerializeObject(ajaxRst));
        }

        //正式删除
        public virtual ActionResult DelByIds1(string Ids)
        {
            bool rst = _baseService.DelByIds1(Ids);
            LayData ajaxRst = new LayData("0", "删除成功");
            return Content(JsonHelp.SerializeObject(ajaxRst));
        }

        public virtual ActionResult GetsAll()
        {
            Expressionable<T> exp = new Expressionable<T>();
            List<T> dataList = _baseService.GetAll(exp);
            LayData ajaxRst = new LayData("0", "获取成功", dataList, dataList.Count);
            return Content(JsonHelp.SerializeObject(ajaxRst));
        }

        /// <summary>
        /// 得到当前登录用户
        /// </summary>
        public Member CurrentUser
        {
            get
            {
                return MemberUtil.CurrentMember;
            }
        }
    }
}